package com.demo.util;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 方法说明: 实现 Runnable 来开启多线程
 * @param null
 * @return
 * @author liangalong[liangalong2022@aliyun.com]
 * @date 2023-04-13 10:58*/
public class Demo5 implements Runnable{

    public int ticktNum = 100;

    Lock lock = new ReentrantLock(true);

    @Override
    public void run() {
        while (true){
            lock.lock();
            if (ticktNum >0){
                try {
                    Thread.sleep(40);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                String name = Thread.currentThread().getName();
                ticktNum--;
                System.out.println(name + " 窗口售票成功，余票"+ ticktNum);
            }
            lock.unlock();
        }
    }
}


